<%@ page language="java" pageEncoding="UTF-8"%>

<%@ include file="/pub/taglibs.inc"%>
<%-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<%@ include file="/pub/head_latest.inc"%>
	<title>我的地址本</title>
	
</head> --%>
<link rel="stylesheet" href="${baseURL}/themes/plugin/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
<style type="text/css">
.mainsearch{ padding-top: 0; }
.down{margin-top:70px;}
</style> 
<!-- <body>
 -->	<div class="FrameWindow">
	<c:if test="${param.openType == 'windows'}">
		<div class="window-menu">
			<div class="menuInner">
				
			</div>
		</div>
		<div class="FrameWindowInner">
	</c:if>
		    <div class="mainsearch cont-table">
			    <table align="center">
					<tr>
						<td rowspan="4" valign="top" style="border:1px solid #e7e4f0;overflow:hidden;">
							<div id="TreeWrap">
								<div class="menu-wrap">
									<c:forEach var="o" items="${allTypes}" varStatus="idx">
					
									<!-- Start -->
									<div id="First_${idx.count}" class="FirstMenu">
										<a href="javascript:void(0)" id="FirstLink_${idx.count}" onclick="ShowLeftMenu('${idx.count}','${o.key}')" > 
						                	<i class="FaMenu fa fa-caret-right"></i> ${o.name}
										</a>
									</div>
									<!-- FirstMenu -->
									
									<div id="Second_${idx.count}" class="SecondMenu" style="display: none">
										<ul id="treeCon${idx.count}" class="ztree"></ul>
									</div>
									</c:forEach>
									<!-- SecondMenu -->
								</div>
							</div>
						</td>
						<td colspan="3" valign="bottom">
							<div class="input-group width-100 padding-left-10">
					             <input type="text" id="orgOrLabSearch" name="orgOrLabSearch" class="form-control border field" placeholder="请输入查询名称" type="text" value="" />
					             <span class="input-group-btn">
					                 <button class="btn btn-normal btn-blue" id="orgOrLabSearchBtn">查询</button>
					             </span>
					         </div><!-- /input-group -->
						</td>
					</tr>
					<tr>
						<td width="30%" class="padding-left-10">
							<div class="select-wrap">
								<div class="header">待选列表</div>
								<ul id="selectCon" class="selecte-list"></ul>
							</div>
						</td>
						<td width="12%" align="center" valign="middle">
							<button class="btn btn-default" id="tabSelectAdd">添加</button>
							<button class="btn btn-default" id="tabSelectDel">删除</button>
							<c:if test="${param.num != 1}">
							<button class="btn btn-default" id="tabSelectAddAll">全部添加</button>
							<button class="btn btn-default" id="tabSelectDelAll">全部删除</button>
							</c:if>
						</td>
						<td width="30%">
							<div class="select-wrap">
								<div class="header">已选列表</div>
								<ul id="selectedCon" class="selecte-list"></ul>
							</div>
						</td>
					</tr>
					<tr>
						<td colspan="3">
							<div class="describe">
								描述：
								<span id="desc"></span>
							</div>
						</td>
					</tr>
				</table>
			</div>
	<c:if test="${param.openType == 'windows'}">
		</div>
	</c:if>
	</div>
<!-- </body> -->

<script type="text/javascript" src="${baseURL}/themes/plugin/zTree_v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${baseURL}/js/addressBook.js"></script>
<script type="text/javascript">
var tabSelect={
	selectArray: [],    //待选列表
	selectedArray: [],  //已选列表
	selectVals: [], 	//选中的待选列表项
	selectedVals: [],   //选中的已选列表项
	
	// 获取勾选的值
	getSelectVal: function(obj,array){
		var _this = tabSelect,
			data = {id: obj.attr('dataid'), text: obj.text(), desc: obj.attr("datadesc"), code : obj.attr("datacode"), param: obj.attr('dataparam')},
			index = _this.getIndex(array, data.id);
		if(index >=0 ){
			array.splice(index,1);
			obj.removeClass('active');
		}else{
			array.push(data);
			obj.addClass('active');
		}
		//console.log('array==='+JSON.stringify(array));
	},
	getIndex: function(array, id){
		for (var i = 0; i < array.length; i++) {
			if(array[i].id == id){
				return i;
			}
		}
		return -1;
	},
	//点击添加按钮添加
	tabSelectAdd: function(){
		(tabSelect.selectVals).forEach(function(data){
			tabSelect.moveItem(0,data);
		})
		tabSelect.selectVals = [];
	},
	//双击添加
	tabSelectDblAdd: function(obj){
		var data = {id: obj.attr('dataid'), text: obj.text(), desc: obj.attr("datadesc"), code :obj.attr("datacode"), param: obj.attr('dataparam')};
		//console.log('data=='+ JSON.stringify(data));
		tabSelect.moveItem(0, data, obj);
	},
	//添加全部
	tabSelectAddAll: function(){
		(tabSelect.selectArray).forEach(function(data){
			tabSelect.moveItem(0,data);
		})
	},
	//点击删除按钮删除
	tabSelectDel: function(){
		(tabSelect.selectedVals).forEach(function(data){
			tabSelect.moveItem(1,data);
		})

		tabSelect.selectedVals = [];
	},
	//双击删除
	tabSelectDblDel: function(obj){
		var	data = {id: obj.attr('dataid'), text: obj.text(), desc: obj.attr("datadesc"), code:obj.attr("datacode"), param: obj.attr('dataparam')};
		tabSelect.moveItem(1, data, obj);
	},
	//删除全部
	tabSelectDelAll: function(){
		var tempArray = $.extend(true,new Array(), tabSelect.selectedArray);
		//console.log(tempArray);
		(tempArray).forEach(function(data){
			tabSelect.moveItem(1,data);
		})
		tabSelect.selectedArray = [];
	},
	//操作item
	moveItem:function(type,data,obj){
		var _this = tabSelect,
			index = _this.getIndex(_this.selectedArray, data.id);
		if(type ==0 ){ //待选->已选
			if(index>=0){ return; }
			if(!obj){
				obj = $('#selectCon').find('li[dataid="'+data.id+'"]');
			}
			obj.hide(); //隐藏 待选列表被选中的li
			obj.removeClass('active');
			(_this.selectedArray).push(data);
			$('#selectedCon').append('<li dataid="'+data.id+'" datadesc="'+ data.desc +'" datacode="'+ data.code +'" dataparam="'+ data.param +'">'+data.text+'</li>');
		}else{ //已选->待选
			if(!obj){
				obj = $('#selectedCon').find('li[dataid="'+data.id+'"]');
			}
			(_this.selectedArray).splice(index,1);						
			obj.remove(); //移除 已选列表被选中的li
			$('#selectCon').find('li[dataid="'+data.id+'"]').show();
		}
	},
	main: function(){
		/* var str=getIds();
			if (str.length > 0){
				var objs = str.split("&");
				for(var i = 0; i < objs.length ; i++){//解析字符串
					var vs = objs[i].split($.sdAddressBook.consts.separator);
					$('#selectedCon').append('<li>'+ vs[1] +'</li>');
				}
			} */
	}
};
$(function(){
	tabSelect.main();
	setSecondMenuMaxHeight();
})
//设置SecondMenu的max-height
function setSecondMenuMaxHeight(){
	var firstMenus = $('#TreeWrap').find('.FirstMenu');
	var maxH = $('#TreeWrap').height()-((firstMenus.height()+1)*firstMenus.length);
	$('#TreeWrap').find('.SecondMenu').css('maxHeight',maxH);
}

$("#orgOrLabSearch").keypress(function(event) {
	if (event.keyCode == 13) { // 13表示回车键
		$("#orgOrLabSearchBtn").click();
	}
});

var pid=null;
var indexType=${defaultChecked};
function ShowLeftMenu(MenuId,type){
	var Control_Menu = '';
	var Control_Link = '';
	Control_Link= "FirstLink_"+MenuId; // 一级菜单 a
	Control_Menu = "Second_"+MenuId; // 二级菜单
	if($("#"+Control_Menu).css("display")=="none"){
		$(".SecondMenu").hide(370);
		$('i.FaMenu').removeClass('fa-caret-down').addClass('fa-caret-right');
		$('i.FaSecond').removeClass('fa-arrow-down-circle').addClass('fa-arrow-right-circle'); 

		$("#"+Control_Link).find('i.FaMenu').removeClass('fa-caret-right').addClass('fa-caret-down');
		$("#"+Control_Menu).show(370).resize();
	}else{
		$("#"+Control_Link).find('i.FaMenu').removeClass('fa-caret-down').addClass('fa-caret-right'); 
		$("#"+Control_Menu).hide(370).resize();
	}
	indexType=type;
};
if(${param.openType == "windows"}){
	$(".cont-table").addClass("down");
	BT.setFormDefaultBtn(FF_ToCancel, FF_ToSave);
}
function FF_ToCancel(){
	window.close();
}
function FF_ToSave(){
	//console.log('已选列表项==='+JSON.stringify(tabSelect.selectedArray));
	var selections=tabSelect.selectedArray;
	if(${param.num <= 0}){
		if(selections.length > 0){
			window.opener.jQuery.zljgAddressBook.winCallback({hasOk : true, data : selections});
			window.close();
		}else{
			BT.showWarning("请选择");
	 	}
	}else{
		if(selections.length == ${param.num}){
			window.opener.jQuery.zljgAddressBook.winCallback({hasOk : true, data : selections});
			window.close();
		}else if(selections.length > 0 && selections.length != ${param.num}){
			BT.showWarning("请选择"+${param.num}+"条数据");
	 	}else{
	 		BT.showWarning("请选择");
	 	}
	}
	
}
function confirm(callback){
	//console.log('已选列表项==='+JSON.stringify(tabSelect.selectedArray));
	var selections=tabSelect.selectedArray;
	if(${param.num <= 0}){
		if(selections.length > 0){
			callback({hasOk : true, data : selections});
		}else{
			BT.showWarning("请选择");
	 	}
	}else{
		if(selections.length == ${param.num}){
			callback({hasOk : true, data : selections});
		}else if(selections.length > 0 && selections.length != ${param.num}){
			BT.showWarning("请选择"+${param.num}+"条数据");
	 	}else{
	 		BT.showWarning("请选择");
	 	}
	}
}
	var setting = {
		view: {
				showIcon: false
		},
		data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "pid",
				rootPId: null
			}
		},
		callback: {
			onClick: zTreeOnClick,
			onExpand: zTreeOnExpand
		},
	};
	
	function zTreeOnExpand(event, treeId, treeNode) {
		var _ul = $("#"+treeNode.tId+"_ul");
		var childLength = _ul.children().length;
		if(childLength == 0){//初次加载
			zTreeOnClick(event, treeId, treeNode);
		}
	};
	function zTreeOnClick(event, treeId, treeNode){
		$("#orgOrLabSearch").val("");
		pid=treeNode.id;
		//$.fn.zTree.getZTreeObj(treeId).expandNode(treeNode);
		if(treeNode.isParent){ //如果是父节点
			var _ul = $("#"+treeNode.tId+"_ul");
			var childLength = _ul.children().length;
			if(childLength == 0){//初次加载
				$.ajax({
			    	type:'post',
			    	url:'${baseURL}/addressbook/tree.do?${queryString}&indexType='+indexType,
			    	data:"nodeId="+pid,
			    	dataType:'json',
			    	success : function(data){
			    		$.fn.zTree.getZTreeObj(treeId).removeChildNodes(treeNode);
			    		$.fn.zTree.getZTreeObj(treeId).addNodes(treeNode,data);
			    	}
			    }) 
			}
		}
		$.getJSON("${baseURL}/addressbookquery/selectnode.do?${queryString}",
 				{nodeId: treeNode.id, indexType: indexType}, function(data){
 						if(data){
 							$("#selectCon").empty();
 							tabSelect.selectArray=[];
 							(data).forEach(function(obj){
 								data = {id: obj.id, text: obj.name, desc:obj.desc, code: obj.code, param: obj.param};
 								tabSelect.selectArray.push(data);
 								$('#selectCon').append('<li dataid="' + obj.id +'" datadesc="'+ obj.desc +'" datacode="'+ obj.code +'" dataparam="'+ obj.param +'">'+ obj.name +'</li>');
 							})   
 						}
 			});
	}
	var zNodes;

	$(document).ready(function(){
		<c:forEach var="o" items="${allTypes}" varStatus="idx">
		$.post("${baseURL}/addressbook/tree.do?${queryString}&indexType=${o.key}",function(data){        
		 	zNodes=eval(data);  
		 	$.fn.zTree.init($("#treeCon${idx.count}"), setting, zNodes);    
		})
		if(${o.key}==${defaultChecked}){
			$("#FirstLink_${idx.count}").find('i.FaMenu').removeClass('fa-caret-right').addClass('fa-caret-down');
			$("#Second_${idx.count}").css("display","block");
		}else{
			$("#Second_${idx.count}").css("display","none");
		}
		</c:forEach>
	});
	
	var clickFlag = true;
	$("#orgOrLabSearchBtn").click(function(){
		if (!clickFlag) return false;
		clickFlag = false;
		var value = $("#orgOrLabSearch").val();
		if($.trim(value).length == 0){
			BT.showWarning("查询名称不能为空！", function(){
				clickFlag = true;
				setTimeout(function(){ $("#orgOrLabSearch").focus(); }, 500);
			});
			return false;
		}
		
		$.ajax({
			url: '${baseURL}/addressbookquery/search.do?${queryString}',
			type: 'get',
			data: {value: value, pid: pid, indexType: indexType},
			dataType: 'json',
			success: function(data){
				if(data){
						$("#selectCon").empty();
						tabSelect.selectArray=[];
						(data).forEach(function(obj){
							data = {id: obj.id, text: obj.name, desc:obj.desc, code:obj.code};
							tabSelect.selectArray.push(data);
							$('#selectCon').append('<li dataid="' + obj.id +'" datadesc="'+ obj.desc +'" datacode="'+ obj.code +'" dataparam="'+ obj.param +'">'+ obj.name +'</li>');
						})   
					}
			},
			complete: function(){
				clickFlag = true;
			}
			
		});
	})

/* 待选列表 */
$('#selectCon').on('click', 'li', function(){
	//console.log($(this));
	tabSelect.getSelectVal($(this),tabSelect.selectVals);
	var desc="";
	for(var i=0;i<tabSelect.selectVals.length;i++){
		for(var j=0;j<tabSelect.selectArray.length;j++){
			if(tabSelect.selectVals[i].id==tabSelect.selectArray[j].id){
				desc+="</br>"+tabSelect.selectArray[j].desc;
				break;
			}
		}
	}
	$("#desc").html(desc);
});
$('#selectCon').on('dblclick', 'li', function(){
	tabSelect.tabSelectDblAdd($(this));
});
/* 已选列表 */
$('#selectedCon').on('click', 'li', function(){
	tabSelect.getSelectVal($(this),tabSelect.selectedVals);
	var desc="";
	for(var i=0;i<tabSelect.selectedVals.length;i++){
		for(var j=0;j<tabSelect.selectedArray.length;j++){
			if(tabSelect.selectedVals[i].id==tabSelect.selectedArray[j].id){
				desc+="</br>"+tabSelect.selectedArray[j].desc;
				break;
			}
		}
	}
	$("#desc").html(desc);
});
$('#selectedCon').on('dblclick', 'li', function(){
	//console.log($(this));
	tabSelect.tabSelectDblDel($(this));
});
//添加
$('#tabSelectAdd').click(function(){
	tabSelect.tabSelectAdd();
});
//全部添加
$('#tabSelectAddAll').click(function(){
	tabSelect.tabSelectAddAll();
});
//删除
$('#tabSelectDel').click(function(){
	tabSelect.tabSelectDel();
});
//全部删除
$('#tabSelectDelAll').click(function(){
	tabSelect.tabSelectDelAll();
});
//保存
$('#saveButton').click(function(){
	//console.log('已选列表项==='+JSON.stringify(tabSelect.selectedArray));
	var selections=tabSelect.selectedArray;
	if(selections && selections.length>0){
		var str = "";
		//var idStr = "";
		if(selections.length == 1){
			str += selections[0].text;
			//idStr += _this.settings.initType+$.sdAddressBook.consts.separator+selections[0].text+$.sdAddressBook.consts.separator+selections[0].value;
		} else {
			for(var i=0;i<selections.length;i++){
				if(i == selections.length-1){
					str += selections[i].text;
					//idStr += _this.settings.initType+$.sdAddressBook.consts.separator+selections[i].text+$.sdAddressBook.consts.separator+selections[i].value;
				} else {
					str += selections[i].text+"、";
					//idStr += _this.settings.initType+$.sdAddressBook.consts.separator+selections[i].text+$.sdAddressBook.consts.separator+selections[i].value+$.sdAddressBook.consts.objSeparator;
				}
			}
		}
		//将数据设置进父窗口相对应的文本域
		//判断是input或者div，不同处理方式
		var txtField = $(window.opener.document.getElementById("${param.txtFieldId}"));
		if(txtField.val){
			txtField.val(str);
		}else{
			txtField.html(str);
		}
		window.close();
	}else{
		BT.showWarning("请选择");
	}
});
$("#closeButton").click(function(){
	window.close();
})

</script>
<!-- </html> -->